﻿//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories
{
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class TagRepository : BaseRepository, ITagRepository
    {
        public TagRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(Tag tag)
        {
            this.GetDbSet<Tag>().Add(tag);
            this.UnitOfWork.SaveChanges();
        }

        public Tag GetTag(Guid tagId)
        {
            return this.GetDbSet<Tag>()
                .Where(c => c.Id == tagId)
                .Single();
        }

        public void Update(Tag updatedTag)
        {
            this.GetDbSet<Tag>().Attach(updatedTag);
            this.SetEntityState(updatedTag, updatedTag.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid tagId)
        {
            Tag tagToDelete = this.GetTag(tagId);
            this.GetDbSet<Tag>().Remove(tagToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Tag> Find()
        {
            return this.GetDbSet<Tag>().Take(100)
                .ToList();
        }

        public IQueryable<Tag> All()
        {
            return this.GetDbSet<Tag>();
        }

        public IEnumerable<Tag> GetAll()
        {
            return this.GetDbSet<Tag>().Take(100)
                .ToList();
        }
    }
}
